package com.fs.jzzn.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import lombok.AllArgsConstructor;
import lombok.Data;

import com.alibaba.fastjson.JSON;
import com.fs.jzzn.pojo.Stock;
import com.fs.jzzn.pojo.Storage;
import com.fs.jzzn.pojo.StorageItem;
import com.fs.jzzn.repository.StockRepository;
import com.fs.jzzn.repository.StorageItemRepository;
import com.fs.jzzn.repository.StorageRepository;

/**
 * @author Administrator
 * @desc 入库相关处理
 */

@AllArgsConstructor
@Data
@Service("storageService")
public class StorageService {
	private StorageRepository storageRepository;
	private StorageItemRepository storageItemRepository;
	private StockRepository stockRepository;
	
	@Transactional
	public void saveOrUpdata(Storage storage){
		/*
		 * 删除历史明细
		 */
		List<StorageItem> storageItems = storageItemRepository.findByBillNo(storage.getBillNo())	;
		storageItemRepository.delete(storageItems);
		
		/*
		 * 保存最新数据
		 */
		List<StorageItem> newStorageItems = JSON.parseArray(storage.getStorageItemJson(),StorageItem.class);
		if(!CollectionUtils.isEmpty(newStorageItems)){
			for(StorageItem item: newStorageItems){
				item.setInOrOut("in");
				item.setBillNo(storage.getBillNo());
				item.setPNo(storage.getPNo());
				item.setContractNo(storage.getContractNo());
			}
			storageItemRepository.save(newStorageItems);
		}
		storageRepository.save(storage);
	}
	
	@Transactional
	public void delete(String id){
		Storage storage = storageRepository.findOne(id);
		List<StorageItem> storageItems = storageItemRepository.findByBillNo(storage.getBillNo())	;
		storageRepository.delete(id);
		storageItemRepository.delete(storageItems);
	}

	@Transactional
	public void confirm(String id) {
		Storage storage = storageRepository.findOne(id);
		List<StorageItem> list = storageItemRepository.findByBillNo(storage.getBillNo());
		List<Stock> stocks = new ArrayList<>();
		
		for(StorageItem item: list){
			Stock stock = null;
			List<Stock>stockList = null;
			if(StringUtils.isNotBlank(item.getMType())){
				stockList = stockRepository.findByMNoAndMNameAndWarehouseNameAndLocationAndMTypeOrderByMCountAsc(item.getMNo(), item.getMName(), item.getWarehouseName(), item.getLocation(),item.getMType());
			}else{
				stockList = stockRepository.findByMNoAndMNameAndWarehouseNameAndLocationOrderByMCountAsc(item.getMNo(), item.getMName(), item.getWarehouseName(), item.getLocation());
			}
			if(CollectionUtils.isEmpty(stockList)){
				stock = new Stock(item.getWarehouseName(), item.getMCount(), item.getLocation(), item.getMNo(), 
						item.getMName(), item.getMType(), item.getMUnit(), item.getMKind(), "", 0);
			}else{
				stock = stockList.get(0);
				stock.setMCount(stock.getMCount() + item.getMCount());
			}
			stocks.add(stock);
		}
		
		/*
		 * 更新库存
		 */
		if(!CollectionUtils.isEmpty(stocks)){
			stockRepository.save(stocks);
		}
		/*
		 * 设置为已入库
		 */
		storage.setStatus("1");
		storageRepository.save(storage);
	}
}
